﻿'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
    //getUserName();
});

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
    //context.load(user);
    //context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
    //$('#message').text('Hello ' + user.get_title());
}

// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
    alert('Failed to get user name. Error:' + args.get_message());
}

function ddlUrl_onchange(ddlUrl) {
    var selectedUrl = $(ddlUrl).val();
    $("#txtRootWebUrl").val(selectedUrl);

    if (selectedUrl === strSPHostUrl) {
        isHostDataRequest = true
    }
    else {
        isHostDataRequest = false
    }

    LoadJSTree(selectedUrl);
}

var strSPAppWebUrl = ""
var strSPHostUrl = ""
var isHostDataRequest = false

function LoadURLDropDown() {
    var mySelect = $('#ddlUrl');

    strSPAppWebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    strSPHostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));

    mySelect.append(
        $('<option></option>').val(strSPAppWebUrl).html("SPAppWebUrl: " + strSPAppWebUrl)
    );
    mySelect.append(
        $('<option></option>').val(strSPHostUrl).html("SPHostUrl: " + strSPHostUrl)
    );

    $("#txtSPAppWebUrl").val(strSPAppWebUrl);
    $("#txtSPHostUrl").val(strSPHostUrl);

    //$("#btnGo").bind("click", function () {
    //    var selectedUrl = $('#ddlUrl').val();
    //    if (selectedUrl === strSPHostUrl) {
    //        isHostDataRequest = true
    //    }
    //    else {
    //        isHostDataRequest = false
    //    }
    //    LoadJSTree();
    //});
}

function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

function GetUrlWithAppContext(oDataUrl) {

    console.log(oDataUrl);

    oDataUrl = oDataUrl.replace(strSPHostUrl, strSPAppWebUrl);
    oDataUrl = StringReplaceIgnoreCase(oDataUrl, "/_api/web", "/_api/SP.AppContextSite(@target)/web");
    oDataUrl = StringReplaceIgnoreCase(oDataUrl, "/_api/site", "/_api/SP.AppContextSite(@target)/site");
    oDataUrl = StringReplaceIgnoreCase(oDataUrl, "/_api/SP.UserProfiles.PeopleManager", "/_api/SP.AppContextSite(@target)/SP.UserProfiles.PeopleManager");
    oDataUrl = StringReplaceIgnoreCase(oDataUrl, "/_api/search", "/_api/SP.AppContextSite(@target)/search");
    oDataUrl = StringReplaceIgnoreCase(oDataUrl, "/_api/publishing", "/_api/SP.AppContextSite(@target)/publishing");
    oDataUrl += "?@target='" + strSPHostUrl + "'";

    console.log(oDataUrl);

    return oDataUrl;
}

function StringReplaceIgnoreCase(text, searchString, repcaseString) {
    var regEx = new RegExp(searchString, "ig");
    var result = text.replace(regEx, repcaseString);

    return result;
}